using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PicAFlick.App
{
    public partial class ImageViewForm : Form
    {
        public ImageViewForm()
        {
            InitializeComponent();
        }

        private PicAFlick.Domain.ImageDetails _imageDetails;

        public PicAFlick.Domain.ImageDetails ImageDetails
        {
            get { return _imageDetails; }
            set { _imageDetails = value; }
        }
        public ImageViewForm(PicAFlick.Domain.ImageDetails id)
        {
            InitializeComponent();
            _imageDetails = id;

        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            _imageDetails.VirtualImage.OnViewportImageUpdated += new PicAFlick.Domain.VirtualImage.ViewPortImageUpdatedHandler(ImageView_OnViewportImageUpdated);
            if (!_imageDetails.FullImageLoaded)
            {
                _imageDetails.LoadImage();
            }
            // this.pictureBox1.Image = _imageDetails.FullImage;
            this.imageNavigator.ImageViewForm = this;


            _imageDetails.VirtualImage.ViewportWindow = new Size(this.imageViewport.Width, this.imageViewport.Height);

            _imageDetails.VirtualImage.FitToViewport();

            this.imageViewport.Image = _imageDetails.VirtualImage.CurrentImageView;
       
            
        }

        void ImageView_OnViewportImageUpdated(object sender, EventArgs e)
        {
            imageViewport.Image = _imageDetails.VirtualImage.CurrentImageView;
            hScrollBar1.Maximum = _imageDetails.VirtualImage.HorizontalMaxViewportTopLeft;
            hScrollBar1.Minimum = _imageDetails.VirtualImage.HorizontalMinViewportTopLeft;
            hScrollBar1.Value = (int)_imageDetails.VirtualImage.HorizontalCurrentTopLeft;

            vScrollBar1.Maximum = _imageDetails.VirtualImage.VerticalMaxTopLeft;
            vScrollBar1.Minimum = _imageDetails.VirtualImage.VerticalMinViewportTopLeft;
            vScrollBar1.Value = (int)_imageDetails.VirtualImage.VerticalCurrentTopLeft;
            vScrollBar1.LargeChange = _imageDetails.VirtualImage.ViewportWindow.Height;
            hScrollBar1.LargeChange = _imageDetails.VirtualImage.ViewportWindow.Width;
        }

        protected override void OnResize(EventArgs e)
        {
            base.OnResize(e);
            this.imageViewport.Width = this.ClientSize.Width;
            this.imageViewport.Height = this.ClientSize.Height - this.button1.Height - 30;
            if (this.ImageDetails != null)
            {
                this.ImageDetails.VirtualImage.ViewportWindow = new Size(this.imageViewport.Width, this.imageViewport.Height);
            }
            this.button1.Top = this.imageViewport.Height;

            this.imageNavigator.Top = this.ClientSize.Height - (30 + this.imageNavigator.Height);
            this.imageNavigator.Left = this.ClientSize.Width - (30 + this.imageNavigator.Width);
            


        }

        private void button1_Click(object sender, EventArgs e)
        {
            
        }

        private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _imageDetails.VirtualImage.ViewportTopLeft = new Point(_imageDetails.VirtualImage.ViewportTopLeft.X, vScrollBar1.Value);
        }

        private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
        {
            _imageDetails.VirtualImage.ViewportTopLeft = new Point(hScrollBar1.Value,_imageDetails.VirtualImage.ViewportTopLeft.Y);
 
        }

        private void imageNavigator_Load(object sender, EventArgs e)
        {

        }



    }
}